package com.beiding.workordersystem.customer.controller;

import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.common.vo.FindByIdsVo;
import com.beiding.workordersystem.customer.entity.CustomerEntity;
import com.beiding.workordersystem.customer.query.CustomerQuery;
import com.beiding.workordersystem.customer.service.CustomerService;
import com.beiding.workordersystem.customer.vo.CustomerEntityVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customer")
public class CustomerController {


    @Autowired
    private CustomerService customerService;

    //保存
    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('customer_save')")
    R save(@RequestBody @Validated CustomerEntityVo vo) {
        CustomerEntity customerEntity = new CustomerEntity();
        BeanUtils.copyProperties(vo, customerEntity);

        customerService.save(customerEntity);
        return R.ok(customerEntity.getId());
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAnyAuthority('customer_delete')")
    R delete(@RequestBody String[] ids) {
        customerService.delete(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('customer_get')")
    R page(MPage page, CustomerQuery customerEntity) {
        return R.ok(customerService.page(page,customerEntity));
    }

    @PostMapping("/findByIds")
    @PreAuthorize("hasAnyAuthority('customer_get')")
    R findByIds(@RequestBody @Validated FindByIdsVo vo) {
        return R.ok(customerService.findByIds(vo, vo.getIds()));
    }

}
